package com.genesis.yunnanji.view;

import android.support.v4.app.Fragment;

import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.BasePagerFragmentAdapter;
import com.genesis.yunnanji.view.fragment.ClassifyFragment;
import com.genesis.yunnanji.view.fragment.HomeFragment;
import com.genesis.yunnanji.view.fragment.MineFragment;
import com.genesis.yunnanji.view.fragment.ShoppingTrolleyFragment;
import com.genesis.yunnanji.widget.NoScrollViewPager;
import com.youth.xframe.widget.XToast;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

/**
 * 创建日期：2018/9/19 on 16:47
 * 作者:Kite
 * 描述:主页面
 */
public class HomePageActivity extends BaseActivity {

    @BindView(R.id.bbi_home)
    BottomBarItem bbiHome;

    @BindView(R.id.bbi_classify)
    BottomBarItem bbiClassify;

    @BindView(R.id.bbi_shopping_trolley)
    BottomBarItem bbiShoppingTrolley;

    @BindView(R.id.bbi_mine)
    BottomBarItem bbiMine;

    @BindView(R.id.bbl)
    BottomBarLayout bbl;

    @BindView(R.id.vp_layout)
    NoScrollViewPager vpLayout;

    private List<Fragment> fragmentList = new ArrayList<>();

    private boolean mBackKeyPressed = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_home_page;
    }

    @Override
    public void initView() {
        super.initView();
        initFragment();
        initListener();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEvent(String str) {
        if (str != null) {
            if (str.equals("trolley")) {
                vpLayout.setCurrentItem(2);
                bbl.onPageSelected(2);
            }
        }
    }

    private void initFragment() {
        fragmentList.add(HomeFragment.newInstance());
        fragmentList.add(ClassifyFragment.newInstance());
        fragmentList.add(ShoppingTrolleyFragment.newInstance());
        fragmentList.add(MineFragment.newInstance());
        BasePagerFragmentAdapter fragmentAdapter = new BasePagerFragmentAdapter(getSupportFragmentManager(), fragmentList);
        vpLayout.setAdapter(fragmentAdapter);
        vpLayout.setOffscreenPageLimit(4);
        vpLayout.setCurrentItem(0);
    }

    private void initListener() {
//                mVpContent.setAdapter(new MyAdapter(getSupportFragmentManager()));
//        bbl.setViewPager(mVpContent);
        bbl.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final BottomBarItem bottomBarItem, int previousPosition, final int currentPosition) {
                vpLayout.setCurrentItem(currentPosition);
                BottomBarItem bottomItem = bbl.getBottomItem(currentPosition);
            }
        });
        bbl.setUnread(0, 20);//设置第一个页签的未读数为20
        bbl.setUnread(1, 99);//设置第二个页签的未读数
        bbl.showNotify(2);//设置第三个页签显示提示的小红点
        bbl.setMsg(3, "NEW");//设置第四个页签显示NEW提示文字

    }

    @Override
    public void onBackPressed() {
        if (!mBackKeyPressed) {
            XToast.warning("再按一次退出程序");
            mBackKeyPressed = true;
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    mBackKeyPressed = false;
                }
            }, 2000);
        } else {
            this.finish();
            System.exit(0);
        }
    }

}
